package edu.tjut.tjutcommunity.controller;


import edu.tjut.tjutcommunity.TjutCommunityApplication;
import edu.tjut.tjutcommunity.entity.Message;
import edu.tjut.tjutcommunity.entity.NoticeMessage;
import edu.tjut.tjutcommunity.entity.Pair;
import edu.tjut.tjutcommunity.entity.User;
import edu.tjut.tjutcommunity.exception.UsernameOrPasswordErrorExecption;
import edu.tjut.tjutcommunity.httpInterface.KxjsHttp;
import edu.tjut.tjutcommunity.service.UserService;
import edu.tjut.tjutcommunity.utils.JWTUtils;
import edu.tjut.tjutcommunity.utils.MessageQueueUtils;
import edu.tjut.tjutcommunity.utils.UserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

import static edu.tjut.tjutcommunity.utils.MessageQueueUtils.checkMessageId;
import static edu.tjut.tjutcommunity.utils.PairUtils.getPair;

@RestController
public class MainController {

    private final static Logger logger = LoggerFactory.getLogger(TjutCommunityApplication.class);


    final
    UserService userService;
    final
    KxjsHttp kxjsHttp;
    final
    MessageQueueUtils messageQueueUtils;


    public MainController(KxjsHttp kxjsHttp, UserService userService, MessageQueueUtils messageQueueUtils) {
        this.kxjsHttp = kxjsHttp;
        this.userService = userService;
        this.messageQueueUtils = messageQueueUtils;
    }

    @GetMapping("/")
    public String index() {
        return "hello";
    }

    //    @CrossOrigin
    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }

    /**
     * 根据用户给的学号和密码访问学校信息门户网站，实现登录。后续为了安全性应该采用密码加密的方式实现传输。
     * @param stuId
     * @param password
     * @return
     */
    @Deprecated
    @PostMapping("/login")
    public Message login(@RequestParam("stu_id") String stuId, @RequestParam("password") String password) {
        User user;
        try {
            user = userService.login(stuId, password);
        }catch (UsernameOrPasswordErrorExecption e){
            return Message.failed("用户名或密码错误。");
        }
        catch (Exception e) {
            e.printStackTrace();
            return Message.failed("登录失败。");
        }
        //生成token
        String token = JWTUtils.createToken(stuId);
        Map<String,Object> map= new HashMap<>();
        map.put("userInfo",user);
        map.put("token",token);

        return Message.ok(map);
    }



    @PostMapping("/getMessage")
    public Message getMessage(@RequestParam(value = "messageId",required = false) String messageId) {
        String stuId = UserUtil.getCurStuId();
        if (messageId == null || "".equals(messageId)) {
            messageId = userService.getUserByStuId(stuId).getMessageId();
        }
        List<NoticeMessage> noticeMessages = messageQueueUtils.readMessage(stuId, messageId);
        if (noticeMessages.isEmpty()) return Message.empty();
        Collections.reverse(noticeMessages);
        return Message.ok(noticeMessages);
    }

    @PostMapping("/ackMessage")
    public Message ackMessage(@RequestParam(value = "messageId",required = false) String messageId) {
        String stuId = UserUtil.getCurStuId();
        User user = userService.getUserByStuId(stuId);
        if(checkMessageId(messageId)){
            user.setMessageId(messageId);
            if(userService.updateUserById(user)){
                return Message.ok();
            }
        }
        return Message.failed("更新失败");
    }




}
